<template>
	<view class="mainPage" :style="{'padding-top': `${$store.state.safeArea.top || 0}px`, 'padding-bottom': `${$store.state.safeArea.bottom || 0}px`}">
		<public-head :title="title"></public-head>
		<public-head :title="title"></public-head>
		<view class="box">
			<view class="box1">
				<view class="top">
					<view class="title">宠e生互联网医院处方笺</view>
					<view class="bianhao-box">
						<view class="bianhao">
							<!-- NO：{{yizhuData.order_sn}} -->
						</view>
						<view class="time">
							{{yizhuData.time}}
						</view>
					</view>
				</view>
			</view>
			<view class="box2">
				<view class="title">
					诊断结果：
				</view>
				<view class="textarea-box">
					<textarea v-model="zhenduanjieguo" placeholder="请输入诊断" placeholder-style="font-size: 26rpx;color: #959595;" />
				</view>
				<view class="tianjia-box">
					<view class="pr">
						RP：
					</view>
				</view>
				<view class="yao-list">
					<view class="yao-item" v-for="(item,index) in yaoList" :key="index">
						<view class="name">
							{{item.drugs}}
							{{item.dr_model}}
						</view>
						<view class="liang">
							用法用量：{{item.explain}}
						</view>
						<view class="liang">
							备注：{{item.remarks}}
						</view>
						<view class="btn-box">
							<text class="shanchu" @click="shanchu(item.drugs_id, index)">
								删除
							</text>
							<text class="bianji" @click="bianji(item.drugs_id, index)">
								编辑
							</text>
						</view>
						<view class="shuliang">
							*{{item.number}}
						</view>
					</view>
				</view>
				<view class="add" @click="add">+ 添加</view>
			</view>
		</view>
		<view class="bottombut">
			<button class="btnfs" @click="share">发送</button>
		</view>
		<u-popup class="u-popup nobgPopup" v-model="show" mode="bottom">
			<view class="anniu-box">
				<view class="queren" @click="qeuren">
					确认
				</view>
				<view class="quxiao" @click="quxiao">
					取消
				</view>
			</view>
		</u-popup>
		<u-popup v-model="showadd" mode="bottom" border-radius="25" height="1216rpx" :closeable="true">
			<view style="background-color: #FFFFFF; height:1216rpx; padding: 104rpx 32rpx;">
				<view class="u-skeleton">
					<view class="box">
						<view class="box1">
							<view class="img">
								<image :src="yaoDetail.dr_img" mode="" />
							</view>
							<view class="info">
								<view class="name">
									{{yaoDetail.dr_name}}
									{{yaoDetail.dr_model}}
								</view>
								<view class="gongsi">
									{{yaoDetail.dr_brand}}
								</view>
								<view class="price">
									¥{{yaoDetail.dr_price}}
								</view>
							</view>
						</view>
						<view class="box2_1">
							<view class="shuliang-box">
								<view class="left">
									<image src="https://swl-1304532176.cos.ap-guangzhou.myqcloud.com/chongesheng_doctor/icon/hua.png" mode="" />
									<text>购买数量（盒）</text>
								</view>
								<view class="right">
									<image @click="jianNum" src="https://swl-1304532176.cos.ap-guangzhou.myqcloud.com/chongesheng_doctor/icon/jian.png" mode="" />
									<text>{{number}}</text>
									<image @click="jiaNum(yaoDetail.dr_stock)" src="https://swl-1304532176.cos.ap-guangzhou.myqcloud.com/chongesheng_doctor/icon/jia.png" mode="" />
								</view>
							</view>
							<view class="beizhu-box">
								<image style="width: 20rpx; display: inline-block; height: 20rpx;" src="https://swl-1304532176.cos.ap-guangzhou.myqcloud.com/chongesheng_doctor/icon/hua.png" mode="" />
								<view class="title">
									用法用量
								</view>
								<textarea v-model.trim="explain" placeholder="请输入用法用量" />
							</view>
							<view class="beizhu-box">
								<view class="title">
									备注
								</view>
								<textarea v-model.trim="remarks" placeholder="请输入药品的备注说明" placeholder-style="font-size: 32rpx;" />
							</view>
						</view>
					</view>
					<view class="btn-box">
						<view class="btn" @click="shiyong">
							使用
						</view>
					</view>
				</view>
			</view>
		</u-popup>
		<poster ref="poster" :drawData="drawData" v-if="zhuanfa" @loading="onLoading" @success="onSuccess" @fail="onFail">
			<template #header></template>
		</poster>
	</view>
</template>
<script>
var that;
import publicHead from "@/components/publicHead/publicHead.vue";
import poster from "../../../components/poster/index.vue";
import { mapState } from "vuex";
export default {
	components: {
		publicHead,
		poster,
	},
	data() {
		return {
			title: "开具处方",
			yizhuData: {
				time: ''
			},
			zhenduanjieguo: "",  //诊断结果
			modify: 0,
			drugs_id: 0,
			show: false,
			showadd: false,
			number: 0,
			yaoDetail: {},
			explain: '',
			remarks: '',
			zhuanfa: false,
			drawData: []
		};
	},
	computed: {
		...mapState(["yaoList"]),
	},
	onLoad(options) {
		that = this;
	},
	onUnload() {
		that.$store.state.yaoList = []
	},
	methods: {
		share() {
			const prescription = that.getPrescription()
			if (!prescription.pets_result) {
				uni.showToast({
					icon: 'none',
					title: '请填写诊断结果'
				})
				return false
			}
			if (!JSON.parse(prescription.rps).length) {
				uni.showToast({
					icon: 'none',
					title: '请添加处方药'
				})
				return false
			}
			that.$postAjax1('Doctorsside_share_prescription', prescription, res => {
				// 医生端  ---- 医生开具可分享处方
				console.log('医生开具可分享处方', res)
				that.drawData = [{
					type: 'image',
					config: {
						url: res.data.data.URL,
						x: 0,
						y: 0,
						w: 300,
						h: 300
					}
				}]
				that.zhuanfa = true
			})
		},
		add() {
			uni.navigateTo({
				url: '/pagesA/workbench/clinicalReception/addDrugs?modify=' + that.modify
			})
		},
		shanchu(drugs_id) {
			that.show = true
			that.drugs_id = drugs_id
		},
		qeuren() {
			console.log("确认")
			that.show = false
			for (var i = 0; i < that.$store.state.yaoList.length; i++) {
				if (that.$store.state.yaoList[i].drugs_id == that.drugs_id) {
					that.$store.state.yaoList.splice(i, 1)
				}
			}
		},
		quxiao() {
			that.show = false
		},
		bianji(drugs_id, index) {
			that.showadd = true
			that.drugs_id = drugs_id
			that.number = that.$store.state.yaoList[index].number
			that.remarks = that.$store.state.yaoList[index].remarks
			that.explain = that.$store.state.yaoList[index].explain
			that.editdrugs()
		},
		editdrugs() {
			that.$postAjax1('Doctorsside_drugs_info', {
				drugs_id: that.drugs_id,
			}, function (data) {  //https://ask.suoweilai.com/Doctorsside_drugs_info  医生端-药品 - 医生端-医生药品详情
				that.yaoDetail = data.data.data
			})
		},
		getPrescription() {
			const pets_result = that.zhenduanjieguo
			const rps = []
			const drugs_array = that.$store.state.yaoList.map(item => {
				console.log(item.drugs_id, item.explain);
				rps.push(item.drugs_id);
				return {
					...item,
					dr_specifications: item.dr_specifications || '',
					dr_model: item.dr_model || ''
				}
			})
			const html_height = 50
			return {
				pets_result,
				rps: JSON.stringify(rps),
				drugs_array: JSON.stringify(drugs_array),
				html_height
			}
		},
		shiyong() {
			if (that.explain == "") {
				uni.showToast({
					icon: "none",
					title: "用法用量不能为空!"
				})
				return
			}
			var data = {
				drugs_id: that.drugs_id,
				drugs: that.yaoDetail.dr_name,
				dr_model: that.yaoDetail.dr_model,
				dr_specifications: that.yaoDetail.dr_specifications,
				number: that.number,
				explain: that.explain,
				remarks: that.remarks,
				Num2: "",
				riNum: 1,
				pianNum: 1,
				neiNum: 1,
			}
			for (var i = 0; i < that.$store.state.yaoList.length; i++) {
				if (that.$store.state.yaoList[i].drugs_id == that.drugs_id) {
					console.log('leixing==2', that.drugs_id)
					that.$store.state.yaoList.splice(i, 1, data)
				}
			}
			console.log(that.$store.state.yaoList, 88888)
			that.showadd = false
		},
		jianNum() {
			if (that.number == 1) {
				return
			}
			that.number--
		},
		jiaNum(dr_stock) {
			if (that.number >= dr_stock) {
				uni.showToast({
					title: '数量超出范围~~',
					icon: 'none',
					duration: 2000
				});
				return
			}
			that.number++
		},
		onLoading() {
			console.log('Loading:正在加载静态资源')
		},
		onSuccess() {
			console.log('Success:创建海报成功')
		},
		onFail(e) {
			console.log('Fail:创建海报失败', e)
		}
	}
};
</script>
<style scoped lang="scss">
.u-skeleton {
	.box1 {
		padding: 20rpx 32rpx;
		display: flex;
		.img {
			image {
				display: block;
				width: 120rpx;
				height: 132rpx;
			}
			margin-right: 12rpx;
		}
		.info {
			.name {
				font-size: 28rpx;
				font-weight: 500;
				color: #333333;
				line-height: 40rpx;
				margin-bottom: 14rpx;
			}
			.gongsi {
				font-size: 24rpx;
				font-weight: 400;
				color: #939499;
				line-height: 30rpx;
				margin-bottom: 14rpx;
			}
			.price {
				font-size: 28rpx;
				font-weight: 400;
				color: #dc1b31;
				line-height: 26rpx;
			}
		}
	}
	.kong {
		width: 100%;
		height: 12rpx;
		background: #f6f6f6;
	}
	.box2_1 {
		.shuliang-box {
			padding: 24rpx 32rpx;
			border-bottom: 2rpx solid rgba(0, 0, 0, 0.08);
			display: flex;
			justify-content: space-between;
			align-items: center;
			.left {
				image {
					display: inline-block;
					width: 20rpx;
					height: 20rpx;
					vertical-align: middle;
					margin-right: 20rpx;
				}
				text {
					font-size: 28rpx;
					font-weight: 400;
					color: #333333;
					line-height: 40rpx;
					vertical-align: middle;
				}
			}
			.right {
				image {
					display: inline-block;
					width: 56rpx;
					height: 56rpx;
					vertical-align: middle;
				}
				text {
					font-size: 28rpx;
					font-weight: 400;
					color: #343434;
					line-height: 40rpx;
					padding: 0 30rpx;
					vertical-align: middle;
				}
			}
		}
		.yongfa-box,
		.yongliang-box,
		.banfa-box {
			padding: 24rpx 0rpx 24rpx 32rpx;
			border-bottom: 2rpx solid rgba(0, 0, 0, 0.08);
			display: flex;
			justify-content: space-between;
			align-items: center;
			display: none;
			.left {
				image {
					display: inline-block;
					width: 20rpx;
					height: 20rpx;
					vertical-align: middle;
					// margin-right: 20rpx;
				}
				text {
					font-size: 28rpx;
					font-weight: 400;
					color: #333333;
					line-height: 40rpx;
					vertical-align: middle;
				}
			}
			.right {
				input {
					width: 120rpx;
					height: 42rpx;
					// border-radius: 4rpx;
					// border: 2rpx solid rgba(0, 0, 0, 0.08);
					// padding-left: 18rpx;
					box-sizing: border-box;
					vertical-align: middle;
					display: inline-block;
					font-size: 24rpx;
					font-weight: 400;
					color: #333;
				}
				.wenben {
					display: inline-block;
					font-size: 28rpx;
					font-weight: 400;
					color: #242424;
					line-height: 40rpx;
					padding: 0 6rpx 0 12rpx;
					vertical-align: middle;
					.shijian {
						padding: 6rpx 20rpx;
						border: 2rpx solid #dcdcdc;
						border-radius: 12rpx;
						margin: 0 4rpx;
						font-size: 26rpx;
						color: #909090;
					}
					.active {
						color: #ffffff;
						background: -webkit-linear-gradient(#ffc347, #ff9331);
					}
				}
				image {
					display: inline-block;
					width: 14rpx;
					height: 20rpx;
					vertical-align: middle;
				}
			}
		}
		.beizhu-box {
			padding: 0 32rpx;
			.title {
				padding: 28rpx 0 20rpx 10rpx;
				font-size: 32rpx;
				font-weight: 400;
				color: #333333;
				line-height: 40rpx;
				display: inline-block;
			}
			textarea {
				width: 100%;
				box-sizing: border-box;
				height: 224rpx;
				// border-radius: 12rpx;
				// border: 2rpx solid #F6F6F6;
				font-size: 32rpx;
				font-weight: 400;
				padding: 24rpx 28rpx;
				line-height: 36rpx;
				color: #959595;
			}
		}
	}
	.rizhouyue {
		width: 376rpx;
		height: 284rpx;
		background: #ffffff;
		border-radius: 12rpx;
		position: absolute;
		top: 418rpx;
		left: 212rpx;
		// z-index: 9999;
		.item {
			padding: 26rpx 40rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			border-bottom: 2rpx solid rgba(0, 0, 0, 0.08);
			&:nth-last-of-type(1) {
				border: none;
			}
			.left {
				font-size: 28rpx;
				font-weight: 400;
				color: #333333;
				line-height: 40rpx;
				vertical-align: middle;
			}
			.right {
				image {
					display: inline-block;
					width: 34rpx;
					height: 34rpx;
					vertical-align: middle;
				}
			}
		}
	}
	.btn-box {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		padding: 0 32rpx 100rpx;
		.btn {
			width: 100%;
			height: 98rpx;
			background: linear-gradient(219deg, #ff9331 0%, #fe7600 100%);
			border-radius: 8rpx;
			font-size: 36rpx;
			font-weight: 400;
			color: #ffffff;
			line-height: 98rpx;
			text-align: center;
		}
	}
}
.box {
	padding-bottom: 200rpx;
	.box1 {
		margin-top: 18rpx;
		padding: 0 32rpx;
		background-color: #ffffff;
		// margin-bottom: 16rpx;
		.top {
			border-bottom: 2rpx solid rgba(0, 0, 0, 0.08);
			.title {
				padding: 12rpx 0 14rpx;
				font-size: 32rpx;
				font-weight: 500;
				color: #343434;
				line-height: 44rpx;
				text-align: center;
			}
			.bianhao-box {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 12rpx;
				.biaohao {
					font-size: 26rpx;
					font-weight: 400;
					color: #a3a3a3;
					line-height: 36rpx;
				}
				.time {
					font-size: 24rpx;
					font-weight: 400;
					color: #a3a3a3;
					line-height: 34rpx;
				}
			}
		}
		.bottom {
			.info-list {
				margin-top: 22rpx;
				display: flex;
				justify-content: space-between;
				flex-wrap: wrap;
				.info-item {
					width: 60%;
					font-size: 28rpx;
					font-weight: 400;
					color: #959595;
					line-height: 40rpx;
					margin-bottom: 28rpx;
					&:nth-of-type(2n) {
						width: 40%;
					}
					text {
						font-size: 26rpx;
						font-weight: 400;
						color: #242424;
						line-height: 36rpx;
					}
				}
			}
		}
	}
	.box2 {
		width: 100%;
		padding: 0 32rpx;
		background: #ffffff;
		padding-bottom: 50rpx;
		.title {
			font-size: 28rpx;
			font-weight: 400;
			color: #242424;
			line-height: 40rpx;
			padding: 22rpx 0 16rpx;
		}
		.textarea-box {
			margin-bottom: 24rpx;
			textarea {
				box-sizing: border-box;
				width: 100%;
				height: 224rpx;
				border-radius: 12rpx;
				border: 2rpx solid #f6f6f6;
				padding: 24rpx 28rpx;
				font-size: 26rpx;
				font-weight: 400;
				color: #333;
				line-height: 36rpx;
				border-radius: 12rpx;
			}
		}
		.tianjia-box {
			display: flex;
			justify-content: space-between;
			padding-bottom: 30rpx;
			.pr {
				font-size: 28rpx;
				font-weight: 400;
				color: #242424;
				line-height: 40rpx;
			}
			.tianjia {
				background: linear-gradient(223deg, #ff9331 0%, #fe7600 100%);
				border-radius: 8rpx;
				font-size: 24rpx;
				font-weight: 400;
				color: #ffffff;
				line-height: 34rpx;
				padding: 12rpx 36rpx;
			}
		}
		.add {
			padding: 16rpx 36rpx;
			// background: linear-gradient(270deg, #FF9331 0%, #FE7600 100%);
			border-radius: 40rpx;
			font-size: 34rpx;
			color: #fe7600;
			border: 1px #fe7600 dashed;
			margin-top: 50rpx;
			width: 100%;
			text-align: center;
		}
		.yao-list {
			.yao-item {
				padding: 40rpx 0 20rpx;
				// border-bottom: 2rpx solid rgba(0,0,0,0.08);
				border-bottom: 3rpx #e8e8e8 solid;
				position: relative;
				&:nth-of-type(1) {
					padding: 0 0 20rpx;
				}
				// &:nth-last-of-type(1){
				// 	border-bottom:none
				// }
				.name {
					font-size: 26rpx;
					font-weight: 500;
					color: #343434;
					line-height: 36rpx;
					margin-bottom: 12rpx;
					width: 90%;
					white-space: nowrap;
					text-overflow: ellipsis;
					overflow: hidden;
				}
				.liang {
					font-size: 24rpx;
					font-weight: 400;
					color: #919398;
					line-height: 38rpx;
				}
				.btn-box {
					text-align: right;
					.shanchu {
						font-size: 24rpx;
						font-weight: 400;
						color: #ffffff;
						line-height: 34rpx;
						padding: 6rpx 36rpx;
						background: #cccccc;
						border-radius: 23rpx;
					}
					.bianji {
						font-size: 24rpx;
						font-weight: 400;
						color: #ffffff;
						line-height: 34rpx;
						padding: 6rpx 36rpx;
						background: linear-gradient(223deg, #ff9331 0%, #fe7600 100%);
						border-radius: 23rpx;
						margin-left: 24rpx;
					}
				}
				.shuliang {
					position: absolute;
					top: 22rpx;
					right: 0;
					font-size: 30rpx;
					font-weight: 400;
					color: #908d8d;
					line-height: 34rpx;
				}
			}
		}
	}
}
.u-popup {
	.anniu-box {
		background-color: transparent;
		padding: 0 22rpx 0rpx;
		// margin-bottom: 50rpx;
		padding-bottom: 50rpx;
		.queren {
			width: 100%;
			height: 106rpx;
			background: #efefef;
			border-radius: 12rpx;
			text-align: center;
			font-size: 32rpx;
			font-weight: 400;
			// color: #E02020;
			line-height: 106rpx;
			margin-bottom: 16rpx;
		}
		.quxiao {
			width: 100%;
			height: 106rpx;
			background: #efefef;
			border-radius: 12rpx;
			text-align: center;
			font-size: 32rpx;
			font-weight: 400;
			line-height: 106rpx;
			// color: #2F78F3;
		}
	}
}
.bottombut {
	background-color: #ffffff;
	position: fixed;
	padding: 30rpx 0 50rpx 0;
	width: 100%;
	bottom: 0;
	.btnfs {
		background: #ff8f2b;
		box-shadow: 0px 6rpx 8rpx 0px rgba(255, 143, 43, 0.2);
		border-radius: 46rpx;
		line-height: 84rpx;
		font-size: 32rpx;
		color: #ffffff;
		width: 80%;
	}
	.btnxf {
		width: 200rpx;
		height: 84rpx;
		background: #ff8f2b;
		box-shadow: 0px 6px 8px 0px rgba(255, 143, 43, 0.2);
		border-radius: 42rpx;
		line-height: 84rpx;
		text-align: center;
		float: right;
		overflow: hidden;
		margin-right: 40rpx;
		color: #ffffff;
		font-size: 32rpx;
	}
}
</style>
